package org.example.service.impl;

import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import org.example.dao.DeptDao;
import org.example.pojo.Dept;
import org.example.service.IDeptService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author TonySong
 * @date 2024/4/25 0025
 * @time 15:23
 */
@Service
@Transactional
public class DeptService implements IDeptService {

    @Resource
    private DeptDao deptDao;
    @Override
    public void save(Dept dept) {
         //saveAndFlush(): 如果有id则进行修改，如果没有id则进行插入
         this.deptDao.saveAndFlush(dept);
    }

    @Override
    public Dept get(Long id) {
        return this.deptDao.findById(id).get();
    }

    @Override
    public List<Dept> list() {
        return this.deptDao.findAll();
    }

    @Override
    public void deleteById(Long id) {
        this.deptDao.deleteById(id);
    }

    @Override
    public boolean hasName(String name) {
        return this.deptDao.countByDname(name) > 0 ;
    }
}
